﻿''' <summary>
''' Display table containing graph series data, WRDB data source data (e.g., BMD data), or shapefile attribute data
''' </summary>
''' <remarks></remarks>
Public Class frmTable
    Private dgvTable As WRDB.Controls.DGVEditor

    Private DataSource As WRDB.DataSource.clsData = Nothing
    Private GISShapefile As DotSpatial.Data.FeatureSet

    Private Sub frmTable_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SaveWindowPos(REGAPP, Me)
    End Sub

    Private Sub frmTable_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            GetWindowPos(REGAPP, Me)
            dgvTable = New WRDB.Controls.DGVEditor(dgTable)
            Refresh()
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Friend Sub SetDataSource(ByRef _DataSource As WRDB.DataSource.clsData)
        DataSource = _DataSource
    End Sub

    Friend Sub SetShapeFile(ByRef _GISShapeFile As DotSpatial.Data.FeatureSet)
        GISShapefile = _GISShapeFile
    End Sub

    Friend Overloads Sub Refresh()

        mnuEditAdd.Enabled = mnuEditEnable.Checked
        mnuEditRemove.Enabled = mnuEditEnable.Checked
        mnuEditRename.Enabled = mnuEditEnable.Checked

        With dgTable
            .SuspendLayout()
            .ReadOnly = Not mnuEditEnable.Checked
            .AllowUserToOrderColumns = False
            .ColumnHeadersVisible = True
            .RowHeadersVisible = True
            .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
            .DefaultCellStyle.Font = New Font(.DefaultCellStyle.Font.FontFamily, 8)
            .ColumnHeadersDefaultCellStyle.Font = New Font(.DefaultCellStyle.Font, FontStyle.Bold)

            'This compiler directive is set in My Project | Compile | Advanced Compile Options | Custom Constants; set it like this: Project="GRAPH"
            'This allows the shared form (frmTable) to display data depending on whether it is compiled for Graph or EPDRiv1

            If DataSource IsNot Nothing Then
                .Columns.Clear()
                .Rows.Clear()
                DataSource.Rewind()
                For Each s As String In DataSource.FieldNames
                    .Columns.Add(s, s)
                Next
                While DataSource.Read And DataSource.NumRead <= 1000 'only show first 1000 records of data table
                    Dim ar(DataSource.FieldNames.Count - 1) As String
                    For i As Integer = 0 To DataSource.FieldNames.Count - 1
                        ar(i) = DataSource.Items(i)
                    Next
                    .Rows.Add(ar)
                End While
                mnuEditEnable.Enabled = False 'do not allow editing
                Text = "WRDB GIS Data Table"
            ElseIf GISShapefile IsNot Nothing Then
                .DataSource = GISShapefile.DataTable
                Text = "WRDB GIS Attribute Table"
            End If

            For c As Integer = 0 To dgTable.ColumnCount - 1
                .Columns(c).SortMode = DataGridViewColumnSortMode.NotSortable
            Next

            .AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells)
            .ResumeLayout()
        End With
    End Sub

    Private Sub mnuEdit_DropDownItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles mnuEdit.DropDownItemClicked
        If e.ClickedItem Is mnuEditEnable Then
            mnuEditEnable.Checked = Not mnuEditEnable.Checked
            Text = Text.Replace(" [Editing]", "") & IIf(mnuEditEnable.Checked, " [Editing]", "")
            Refresh()
        Else
            With New frmEditTable(GISShapefile, mnuEdit.DropDownItems.IndexOf(e.ClickedItem) - 2)
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    Refresh()
                End If
            End With
        End If
    End Sub

    Private Sub btnClose_Click(sender As Object, e As System.EventArgs) Handles btnClose.Click
        If mnuEditEnable.Checked Then
            dgTable.EndEdit(DataGridViewDataErrorContexts.Commit)
            GISShapefile.DataTable.AcceptChanges()
            GISShapefile.Save()
        End If
        Close()
    End Sub
End Class